package com.quarters.serviceImpl;

import com.quarters.bean.Article;
import com.quarters.bean.User;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.layuiBean.VoPage;
import com.quarters.bean.request.NewOrderTemplatData;
import com.quarters.bean.request.Pay;
import com.quarters.bean.request.Region;
import com.quarters.bean.request.SendArticles;
import com.quarters.mapper.ArticleMapper;
import com.quarters.mapper.InquiryMapper;
import com.quarters.mapper.UserMapper;
import com.quarters.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quarters.service.InquiryService;
import com.quarters.service.XbxWxService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.quarters.serviceImpl.InquiryServiceImpl.stampToDate;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-03
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    XbxWxService xbxWxService;

    /**
     * @param voPage
     * @return 后台查询
     */
    @Override
    public ResultPage<Article> selectMyPage(VoPage voPage,Long userInfoId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("userInfoId", userInfoId);
        List<Article> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<Article> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public ResultPage<Article> selectMyPageAll(VoPage voPage ) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<Article> list = baseMapper.selectMyPageAll(paramMap);
        Integer count = baseMapper.countMyPageAll(paramMap);
        ResultPage<Article> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }


    /**
     * 发送模板消息
     */
    @Override
    public void sendArticlesUser(String userId,Long createTime,Long articleId,String title) {
        String userIds[] = userId.split(",");
            for (int i = 0; i < userIds.length; i++) {
                User user = userMapper.selectByIdUser(Long.parseLong(userIds[i]));
                //给所在小区的管理员发送模板消息
                SendArticles sendArticles = new SendArticles();
                System.out.println("22222:"+title);
                //推送标题
                sendArticles.setTiele(title);
                //创建时间
                sendArticles.setCreateTime(stampToDate(createTime + ""));
                //推送人
                sendArticles.setToUser(user.getOpenid());
                //w文章id
                sendArticles.setArticleId(articleId);
                try {
                    xbxWxService.sendArticles(sendArticles);
                } catch (WxErrorException e) {
                    e.printStackTrace();
                }
            }
    }




    /**
     * 给改管理员的上级(区域管理员)发送模板消息提示进行审核
     */
    @Override
    public void examine(Long userId,Long createTime,String orderNumber) {
        //根据当前用户的id查找该用户的上级(区域管理员)
        User user = userMapper.selectRegion(userId);
        System.out.println("改管理员的区域管理员是:"+user);
        Region region = new Region();
        //内容
        region.setTiele("请登陆后台管理审核文章");
        //时间
        region.setCreateTime(stampToDate(createTime + ""));
        //发给谁
        region.setToUser(user.getOpenid());
        try {
                xbxWxService.region(region);
            } catch (WxErrorException e) {
                e.printStackTrace();
            }
    }

    /**
     * 管理员发送文章
     */
    @Override
    public void send(String userId,String title,Long articleId,Long createTime) {
        String userIds[] = userId.split(",");
        for (int i = 0; i < userIds.length; i++) {
            User user = userMapper.selectByIdUser(Long.parseLong(userIds[i]));
            //给所在小区的管理员发送模板消息
            SendArticles sendArticles = new SendArticles();
            //推送标题
            sendArticles.setTiele(title);
            //创建时间
            sendArticles.setCreateTime(stampToDate(createTime + ""));
            //推送人
            sendArticles.setToUser(user.getOpenid());
            //w文章id
            sendArticles.setArticleId(articleId);
            try {
                xbxWxService.sendArticles(sendArticles);
            } catch (WxErrorException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 给管理员发送模板消息进行支付
     */
    @Override
    public void pay(Long userId,Long createTime,String title,String orderMoeny,String orderNumber,int flag ) {
        //根据当前用户的id查找该用户的上级(区域管理员)
        User user = userMapper.selectOpenId(userId);
        System.out.println("支付管理员是:"+user);
        Pay pay = new Pay();
        //内容
        pay.setTitle(title);
        //时间
        pay.setCreateTime(stampToDate(createTime + ""));
        //发给谁
        pay.setToUser(user.getOpenid());
        //金额
        BigDecimal num1 = new BigDecimal(100);
        BigDecimal num2 = new BigDecimal(orderMoeny);
        BigDecimal result3 = num2.divide(num1);
        pay.setOrderMoeny(result3+"元");
        //订单号
        pay.setOrderNumber(orderNumber);
        //标识
        pay.setFlag(flag);
        try {
            xbxWxService.pay(pay);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }




}
